package common.display
{
    import common.net.URLActionImagesManager;
    
    import config.Path;
    
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.Event;
    
    import kernel.cdn.CDN;
    
    import mx.utils.StringUtil;
    
    import org.granite.net.event.AssetEvent;

    public class ActionImageView extends Sprite
    {
        private static const PI2:Number = Math.PI * 2;
        private var _canvas:Bitmap;
        private var currentPath:String = "";
        private var _angle:Number;

        private var _animation:ActionImage;
        private var _column:int;
        private var _directionAmount:int;
        private var _row:int;
        private var _rowIndex:int;
        private var _totalFrames:int;
        private var _totalImages:uint;

        private var _currentTime:uint;

        public function ActionImageView(url:String = null)
        {
            _canvas = new Bitmap ();
            this.addChild (_canvas);
            if (url)
            {
                load (url);
            }
        }

        public static function create(sex:int , actionId:int , key:String = ""):ActionImageView
        {


            var aciw:ActionImageView = new ActionImageView (testurl (sex , actionId , key));
            return aciw;
        }

        public static function testurl(sex:int , actionId:int , key:String = ""):String
        {
            var path:String;
            if (key == "" || key == null)
            {
                path = Path.getDefaultActionImage (sex , actionId);
            }
            else
            {
				var strId:String = actionId.toString(16);
				while(strId.length<2)
				{
					strId = "0"+strId;
				}
                path = Path.parseUrl (key+strId);
            }
            return path;
        }

        public function load(url:String):void
        {
            if (url != currentPath)
            {
                clearImage ();
                currentPath = url;
                doload (currentPath);
            }
        }

        public function setActionImage(value:ActionImage , currentFrame:int = 0):void
        {
            if (value)
            {
				_animation = value;
                _totalImages = value.directionCount * value.frameCount;
                _totalFrames = value.frameCount;
                _directionAmount = value.directionCount;
                currentFrame %= _totalFrames;
                _column = currentFrame;
                refres ();
                _currentTime = 0;
                this.addEventListener (Event.ENTER_FRAME , onEnterFrame);
            }
            else
            {
                clearImage ();
            }
        }

        public function get angle():Number
        {
            return _angle;
        }

        public function set angle(value:Number):void
        {
            _angle = value;
            if (!_animation)
            {
                return;
            }

            var row:int = getDirection (_angle , _directionAmount);
            if (_row != row)
            {
                _row = row;
                refres ();
            }
        }



        public function gotoFrame(frame:int):void
        {
            if (_animation)
            {
                frame %= _totalFrames;
                _column = frame;
                refres ();
            }
        }

        public function destory():void
        {
            clearImage ();
        }

        private function doload(path:String):void
        {
            URLActionImagesManager.instance.addDataListener (path , onRetPreviewImage);
            URLActionImagesManager.instance.reference (path);
        }

        private function refres():void
        {
            if (_animation != null)
            {
                _rowIndex = _row * _totalFrames;
                var index:uint = _rowIndex + _column;

                var currentImage:ActionImageData = _animation.getActionImageByIndex (index);
                _canvas.bitmapData = currentImage.images;
                _canvas.x = currentImage.offsetX;
                _canvas.y = currentImage.offsetY;
            }
        }

        private function clearImage():void
        {
            if (currentPath != "")
            {

                if (URLActionImagesManager.instance.hasEventListener (currentPath))
                {
                    URLActionImagesManager.instance.removeDataListener (currentPath , onRetPreviewImage);
                }
                URLActionImagesManager.instance.free (currentPath);
                currentPath = "";

            }
            if (_animation)
            {
                this.removeEventListener (Event.ENTER_FRAME , onEnterFrame);
                _animation = null;
                this._canvas.bitmapData = null;
            }
        }

        private function onRetPreviewImage(event:AssetEvent):void
        {
            URLActionImagesManager.instance.removeDataListener (event.type , onRetPreviewImage);
            var ai:ActionImage = event.data as ActionImage;
            if (ai != null)
            {
                setActionImage (ai);
            }
        }

        private function onEnterFrame(event:Event):void
        {
            _currentTime++;
            if ((_currentTime % 3) == 0)
            {
                gotoFrame (_currentTime / 3);
            }
        }

        private function getDirection(angle:Number , directionAmount:int):int
        {
            var a0:Number = PI2 / directionAmount;
            return int (((angle + a0 / 2) % PI2) / a0);
        }


    }

}
